<?php
namespace app\myr\controller;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use app\myr\model\LotteryActive as LotteryActiveModel;



/**
 * 抽奖管理
 */
class LotteryActive extends Controller
{
    public $table = 'LotteryActive';
    public $configtable = 'LotteryConfig';

    public function index(){
        LotteryActiveModel::mQuery()->layTable(function () {
            $this->title = '抽奖管理';
        }, static function (QueryHelper $query) {
            
        });
    }

    /**
     * 新增抽奖
     */
    public function add(){
        if (!$this->request->isPost()){
            return view('form');
        }else{
            //post提交数据
            $data=$this->request->post();
            ($data['active_name'] == '') && $this->error('活动名称不能为空');
            $data['add_time']=date("Y-m-d H:i:s");
            $result=Db::connect($this->conn)->name($this->table)->insert($data);
            if($result){
                $this->success('新增抽奖成功','');
            }else{
                $this->error('抽奖添加失败','');
            }
        }
    }

     /**
     * 编辑抽奖
     */
    public function edit(){
        $id = $this->request->get('id');
        if (!$this->request->isPost()){
            $vo=Db::connect($this->conn)->name($this->table)->where("id",$id)->find();
            return view('form',['vo'=>$vo]);

        }else{
            //post提交数据
            $data=$this->request->post();
            ($data['active_name'] == '') && $this->error('活动名称不能为空');
            $result=Db::connect($this->conn)->name($this->table)->update($data);
            if($result){
                $this->success('保存数据成功','');
            }else{
                $this->error('数据保存失败','');
            }
        }
    }
    
    /**
     * 软删除抽奖
     */
    public function del()
    {
        if ($this->request->isPost()){
           $data= $this->request->post();
           $up["is_delete"]=1;
           $res=Db::connect($this->conn)->name($this->table)->where(array("id"=>$data["id"]))->update($up);
           if($res){
               $this->success('抽奖删除成功','');
           }else{
               $this->error('抽奖删除失败','');
           }
        }
        $this->error("抽奖删除失败, 请稍候再试!");
    }

}